<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
import '@/assets/css/FancyStyle.css'
import '@/assets/css/fancySearchForm.css'
const categorys = ref([
    {
        "id": 3,
        "categoryName": "美食",
        "categoryAlias": "my",
        "createTime": "2023-09-02 12:06:59",
        "updateTime": "2023-09-02 12:06:59"
    },
    {
        "id": 4,
        "categoryName": "娱乐",
        "categoryAlias": "yl",
        "createTime": "2023-09-02 12:08:16",
        "updateTime": "2023-09-02 12:08:16"
    },
    {
        "id": 5,
        "categoryName": "军事",
        "categoryAlias": "js",
        "createTime": "2023-09-02 12:08:33",
        "updateTime": "2023-09-02 12:08:33"
    }
])
import {
  articleCategoryListService,
  articleCategoryAddService,
  articleCategoryUpdateService,
  articleCategoryDeleteService,
} from '@/api/article.js'
const articleCategoryList = async()=>{
    let result = await articleCategoryListService();
    categorys.value = result.data;
}
articleCategoryList()

//添加分类数据模型
const categoryModel = ref({
    categoryName: '',
    categoryAlias: ''
})
//添加分类表单校验
const rules = {
    categoryName: [
        { required: true, message: '请输入分类名称', trigger: 'blur' },
    ],
    categoryAlias: [
        { required: true, message: '请输入分类别名', trigger: 'blur' },
    ]
}

import {ElMessage,ElMessageBox} from 'element-plus'
const addCategory = async()=>{
    let result = await articleCategoryAddService(categoryModel.value);
    ElMessage.success(result.msg ? result.msg:'添加成功');
    articleCategoryList();
    dialogVisible.value=false;
}

const title = ref('')
const dialogVisible=ref('')
dialogVisible.value=false
const showDialog = (row)=>{
    dialogVisible.value=true;

    title.value='编辑分类';
    categoryModel.value.categoryName = row.categoryName;
    categoryModel.value.categoryAlias = row.categoryAlias;
    categoryModel.value.id = row.id;
}



const updateCategory = async()=>{
    let result = await articleCategoryUpdateService(categoryModel.value);
    ElMessage.success(result.msg? result.msg:'编辑成功');
    articleCategoryList();
    dialogVisible.value=false;
}

const clearData = ()=>{
    dialogVisible.value=true;
    title.value='添加分类';
    categoryModel.value.categoryName = '';
    categoryModel.value.categoryAlias = '';
}
const deleteCategory = (row)=>{
    ElMessageBox.confirm(
    '你确定要删除该分类吗?',
    '温馨提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      let result = await articleCategoryDeleteService(row.id)
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      articleCategoryListService();
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消操作',
      })
    })
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>文章分类</span>
                <div class="extra">
                    <el-button type="primary" @click="clearData">添加分类</el-button>
                </div>
            </div>
        </template>
<!-- 文章分类 -->
  <el-table
    :data="categorys"
    class="custom-table"
    style="width: 100%"
    border
    highlight-current-row
  >
     <el-table-column label="序号" width="200" type="index">
      <template #default="{ $index }">
        <span class="tag tag-index">{{ $index + 1 }}</span>
      </template>
    </el-table-column>

    <el-table-column label="分类名称" width="380" prop="categoryName" header-class-name="header-green">
      <template #default="{ row }">
        <span class="tag tag-blue">{{ row.categoryName }}</span>
      </template>
    </el-table-column>

    <el-table-column label="分类别名" width="380" prop="categoryAlias" header-class-name="header-purple">
      <template #default="{ row }">
        <span class="tag tag-orange">{{ row.categoryAlias }}</span>
      </template>
    </el-table-column>

    <el-table-column label="操作" width="380" header-class-name="header-orange">
      <template #default="{ row }">
        <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)" />
        <el-button :icon="Delete" circle plain type="danger" @click="deleteCategory(row)" />
      </template>
    </el-table-column>
  </el-table>

 
    <!-- 抽屉表单 -->


<el-dialog
  v-model="dialogVisible"
  :title="title"
  width="30%"
  class="fancy-dialog"
>
  <el-form
    :model="categoryModel"
    :rules="rules"
    label-width="100px"
    class="fancy-form"
    style="padding-right: 30px"
  >
    <el-form-item label="分类名称" prop="categoryName">
      <el-input
        v-model="categoryModel.categoryName"
        class="fancy-input"
        placeholder="请输入分类名称"
      />
    </el-form-item>

    <el-form-item label="分类别名" prop="categoryAlias">
      <el-input
        v-model="categoryModel.categoryAlias"
        class="fancy-input"
        placeholder="请输入分类别名"
      />
    </el-form-item>
  </el-form>

  <template #footer>
    <span class="dialog-footer">
      <el-button class="fancy-btn cancel" @click="dialogVisible = false">取消</el-button>
      <el-button class="fancy-btn confirm" @click="title=='添加分类'? addCategory() : updateCategory()">确认</el-button>
    </span>
  </template>
</el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}


</style>